<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:product:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:product:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:product:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:product:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品类型" align="center" prop="productType" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品规格" align="center" prop="productSpecs" />
      <!-- <el-table-column label="bom" align="center" prop="bomId" /> -->
      <el-table-column
        label="BOM"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handlelook(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>

      <el-table-column
        label="工艺"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handlecraft(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="userId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:product:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:product:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入产品类型" />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="产品规格" prop="productSpecs">
          <el-input v-model="form.productSpecs" placeholder="请输入产品规格" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="父产品编码" prop="parentProNum">
          <el-input
            v-model="form.parentProNum"
            placeholder="请输入父产品编码"
          />
        </el-form-item>
        <el-form-item label="仓库id" prop="depotId">
          <el-input v-model="form.depotId" placeholder="请输入仓库id" />
        </el-form-item>
        <el-form-item label="库存数量" prop="invenNum">
          <el-input v-model="form.invenNum" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker
            clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--查看bom详情 -->
    <el-dialog title="BOM详情" :visible.sync="detailVisible" width="50%">
      <el-form  label-width="80px">
          <el-form-item label="产品名称:">
            <el-input  v-model="this.tableProduct.productName" readonly/>
          </el-form-item>
        <el-form-item label="产品编码:">
          <el-input v-model="this.tableProduct.productCode"  readonly/>
        </el-form-item>
        <el-form-item label="产品类型:">
          <el-input v-model="this.tableProduct.productType"  readonly/>
        </el-form-item>
        <el-form-item label="BOM编码:" >
          <el-input v-model="this.tableBom.bomCode" readonly/>
        </el-form-item>
        <el-form-item label="物料清单:">
        </el-form-item>
         <!-- 物料子表 -->
       <el-table
    :data="tablebommat"
    style="width: 100%"
    max-height="250">
    <el-table-column
      fixed
      type="index"
      label="序号"
      width="150">
    </el-table-column>
    <el-table-column
      prop="materialName"
      label="物料名称"
      width="120">
    </el-table-column>
    <el-table-column
      prop="needMatNum"
      label="物料数量"
      width="120">
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="120">
      <template slot-scope="scope">
        <el-button
          @click.native.prevent="deletebom(scope.$index, tablebommat)"
          type="text"
          size="small">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 查看工艺详情 -->
    <el-dialog  title="工艺详情" :visible.sync="craftopen" width="50%">
      <el-form  label-width="80px">
          <el-form-item label="产品名称:">
            <el-input v-model="this.tableProduct.productName"  readonly/>
          </el-form-item>
        <el-form-item label="产品编码:">
          <el-input  v-model="this.tableProduct.productCode" readonly/>
        </el-form-item>
        <el-form-item label="工艺名称:">
          <el-input v-model="this.tablecartft.craftName"  readonly/>
        </el-form-item>
        <el-form-item label="工艺编码:">
          <el-input v-model="this.tablecartft.craftCode" readonly/>
        </el-form-item>
        <el-form-item label="工序:">
        </el-form-item>
           <!-- 工序子表 -->
        <table class="table table-bordered table-hover table-striped">
          <thead>
            <tr>
              <th scope="col">工序号</th>
              <th scope="col">工序编码</th>
              <th scope="col">工序名称</th>
              <th scope="col">原材料</th>
              <th scope="col">设备</th>
              <th scope="col">是否报工</th>
              <th scope="col">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in tableProcess" :key="item.processNum">
              <td> {{item.processNum}} </td>
              <td>{{ item.processCode }}</td>
              <td>{{ item.processName }}</td>
              <td>{{ item.materialType }}</td>
              <td>{{ item.tool }}</td>
              <td>
                <select name="" id="">
                  <option value="">是</option>
                  <option value="">否</option>
                </select>
              </td>
              <td>
                <a href="javascript:;" @click="remove(item.id)">删除</a>
              </td>
            </tr>
          </tbody>
        </table>

      </el-form>
      <!-- <div>
        <h3>产品编码：{{ this.tableBom.bomCode }}</h3>
      </div>-->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
         
    </el-dialog>
  </div>
</template>

<script>
import {listProduct, getProduct,delProduct,addProduct,updateProduct,bommaterial} from "@/api/mes/product";
import { lookBOM } from "@/api/mes/BOM";
import {lookcraft,lookprocess} from "@/api/mes/craft"

export default {
  name: "Product",
  data() {
    return {
      value:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailVisible:false,
      craftopen:false,
      tableBom:"",
      tablecartft:"",
      protable:"",
      tableProcess:[],
      //产品详情
      tableProduct:"",
      //bom对应物料
      tablebommat:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" },
        ],
        productCode: [
          { required: true, message: "产品编码不能为空", trigger: "blur" },
        ],
      },
      tableData: [{
          index: '1',
          name: '管道',
          code: '202222',
          num: '13',
          material:'',
          work:'',
          tool:''
        }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //删除bom物料
    deletebom(index, rows) {
      rows.splice(index, 1);
    },
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productType: null,
        productCode: null,
        productSpecs: null,
        status: null,
        parentProNum: null,
        bomId: null,
        craftId: null,
        depotId: null,
        invenNum: null,
        price: null,
        note: null,
        createDate: null,
        userId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品管理";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProduct(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增bom物料信息
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
    /** 查看BOM详情 */
    handlelook(row) {
      this.detailVisible = true;
      // 查询bom
      lookBOM(row.id).then((response) => {
        console.log(response.data);
        this.tableBom = response.data;
        this.title = "BOM详情";
      });
      //查询产品
      getProduct(row.id).then((response)=>{
        console.log(response.data);
        this.tableProduct = response.data;
      })
      //查询bom对应的物料
      bommaterial(row.bomCode).then((response)=>{
        console.log(response.data);
        this.tablebommat = response.data
      })
    },

    // 查看工艺详情
    handlecraft(row) {
      this.craftopen = true;
      lookcraft(row.id).then((response) => {
        console.log(response.data);
        this.tablecartft = response.data;
        this.title = "工艺详情";
      });
      getProduct(row.id).then((response)=>{
        console.log(response.data);
        this.tableProduct = response.data;
      });
      lookprocess(row.craftId).then((response)=>{
        console.log(response);
        this.tableProcess = response.rows;
      })
    }
  },
};
</script>

<style scoped>
@import url(../../../css/bootstrap.css);
</style>